package com.springboot.demo.mapper;

import com.springboot.demo.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-08-16 15:48
 */
public interface RoleMapper {

    /**
     * rid =1 为超级管理员
     */
    @Select("select distinct r.id from web_role r left join web_role_menu_group rmg on r.id = rmg.roleId left join " +
            "web_user_role ur on ur.rid = r.id where " +
            "menuGroupId = #{menuGroupId} and ur.uid = #{adminId} or r.id = 1")
    List<Role> getRolesByMenuGroupIdAndAdminId(Long menuGroupId,Long adminId);

    @Select("select r.id,r.name,r.description from web_role r,web_user_role ur where r.id = ur.rid and uid = #{id}")
    List<Role> getUserRolesByUserId(Long uid);

    @Select("select id,name,description from web_role where name = #{name}")
    Role getRoleByName(String name);

    @Insert("insert into web_role set name = #{name},description=#{description}")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void saveRole(Role role);

    @Select("select id,name,description from web_role where id = #{id}")
    Role getRoleById(Long id);

    @Delete("delete from web_role where id = #{id}")
    void deleteRoleById(Long id);

    @Update("update web_role set name = #{name},description=#{description} where id = #{id}")
    void updateRole(Role role);

    @Select("select id,name,description from web_role")
    List<Role> getAllRoles();

    @Select("select id,name,description from web_role where id = #{rid} and id in (select rid from web_user_role where uid = #{uid})")
    Role getRoleByUidAndRid(Long uid, Long rid);

    @Select("select id from web_user_role where rid = #{rid}")
    Long getUserRolesByRid(Long rid);

    @Select("select r.id,r.name,r.description from web_role r,web_user_role ur where r.id = ur.rid and name = #{name}" +
            " and uid = #{id}")
    Role getRoleByUidAndName(Long id, String name);

    @Select("select uid from web_user_role where rid = #{rid} and uid = #{uid}")
    Long getUidByUidAndRid(Long uid, Long rid);

    @Select("select rid from web_user_role where uid = #{uid} order by rid")
    List<Long> getRoleIdByUid(Long uid);

    @Select("select id,name,description from web_role where name = #{role} and id in (select rid from web_user_role where uid = " +
            "#{uid})")
    Role getRoleByUidAndRole(Long uid,String role);

    @Select("select uid from web_user_role where rid = #{rid}")
    List<Long> getUserIdByRid(Long rid);
}
